package com.py.credit.controller;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.service.RuleBindingService;
import com.py.framework.core.annotation.IgnoreAuthority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/ruleBinding")
public class RuleBindingController {

    // 没有启用版本的提示语
    private static final String NO_VERSION_MSG = "没有启用的发布版本";

    @Autowired
    private RuleBindingService ruleBindingService;

    /**
     * 表规则绑定
     *
     * @param rule_id
     * @param table_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addRuleBinding.json")
    @Deprecated
    public RestServiceResult<Object> add(
            @RequestParam(defaultValue = "", required = true, value = "rule_id") int rule_id,
            @RequestParam(defaultValue = "", required = true, value = "table_id") int table_id) {
        RestServiceResult<Object> result = new RestServiceResult<>();
        // 获取当前版本信息，没有则提示
        Integer version = ruleBindingService.selectCurrentContentVersion();
        if (null == version || version < 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(NO_VERSION_MSG);
            return result;
        }
        ruleBindingService.addTableRuleBinding(rule_id, table_id, version);
        return result;
    }

    /**
     * 字段规则绑定
     *
     * @param rule_id
     * @param filed_code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addColmnBinding.json")
    @Deprecated
    public RestServiceResult<Object> addColmnBinding(
            @RequestParam(defaultValue = "", required = true, value = "rule_id") int rule_id,
            @RequestParam(defaultValue = "", required = true, value = "filed_code") String filed_code,
            @RequestParam(defaultValue = "", required = true, value = "extend") String extend) {
        RestServiceResult<Object> result = new RestServiceResult<>();
        // 获取当前版本信息，没有则提示
        Integer version = ruleBindingService.selectCurrentContentVersion();
        if (null == version || version < 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(NO_VERSION_MSG);
            return result;
        }
        ruleBindingService.addColmnBinding(rule_id, filed_code, extend, version);
        return result;
    }

    /**
     * @api {get} /ruleBinding/updateTableBinding.json 表规则绑定和解绑操作
     * @apiName updateTableBinding
     * @apiVersion 0.0.1
     * @apiGroup 规则绑定
     * @apiParam {long} id 规则绑定关系ID
     * @apiParam {int} rule_id 规则ID
     * @apiParam {int} table_id 表ID
     * @apiParam {String} extend 扩展
     * @apiParam {String} isDel 是否删除 Y:是 N:否
     * @apiParam {String} type 表规则字段类型 T:时间规则 N:普通规则
     * @apiSuccess {String} code 状态码:1 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     */
    @ResponseBody
    @RequestMapping(value = "/updateTableBinding.json")
    public RestServiceResult<Object> updateTableBinding(
            @RequestParam(defaultValue = "", required = true, value = "id") Long id,
            @RequestParam(defaultValue = "", required = true, value = "rule_id") int rule_id,
            @RequestParam(defaultValue = "", required = true, value = "table_id") int table_id,
            @RequestParam(defaultValue = "", required = true, value = "extend") String extend,
            @RequestParam(defaultValue = "", required = true, value = "isDel") String isDel) {
        RestServiceResult<Object> result = new RestServiceResult<>();
        // 获取当前版本信息，没有则提示
        Integer version = ruleBindingService.selectCurrentContentVersion();
        if (null == version || version < 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(NO_VERSION_MSG);
            return result;
        }
        ruleBindingService.updateTableBinding(id, rule_id, table_id, extend, isDel, version);
        return result;
    }

    /**
     * @api {get} /ruleBinding/updateColmnBinding.json 更新字段绑定规则，id不存在插入，否则更新
     * @apiName updateColmnBinding
     * @apiVersion 0.0.1
     * @apiGroup 规则绑定
     * @apiParam {long} id 绑定关系ID
     * @apiParam {int} rule_id 绑定规则ID
     * @apiParam {String} filed_code 字段CODE
     * @apiParam {String} extend 扩展
     * @apiParam {String} isDel 是否删除 Y:是 N:否
     * @apiSuccess {String} code 状态码:1 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     */
    @ResponseBody
    @RequestMapping(value = "/updateColmnBinding.json")
    public RestServiceResult<Object> updateColmnBinding(
            @RequestParam(defaultValue = "", required = true, value = "id") Long id,
            @RequestParam(defaultValue = "", required = true, value = "rule_id") int rule_id,
            @RequestParam(defaultValue = "", required = true, value = "filed_code") String filed_code,
            @RequestParam(defaultValue = "", required = true, value = "extend") String extend,
            @RequestParam(defaultValue = "", required = true, value = "isDel") String isDel) {
        RestServiceResult<Object> result = new RestServiceResult<>();
        // 获取当前版本信息，没有则提示
        Integer version = ruleBindingService.selectCurrentContentVersion();
        if (null == version || version < 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(NO_VERSION_MSG);
            return result;
        }
        ruleBindingService.updateColmnBinding(id, rule_id, filed_code, extend, isDel, version);
        return result;
    }

    @IgnoreAuthority
    @RequestMapping(value = "/bindTableRule.json")
    @ResponseBody
    public RestServiceResult<Object> bindTableRule(
            @RequestParam(defaultValue = "", required = true, value = "ruleId") int ruleId,
            @RequestParam(defaultValue = "", required = true, value = "extend") String extend,
            @RequestParam(defaultValue = "", required = true, value = "tableId") String tableId) {
        RestServiceResult<Object> result = new RestServiceResult<>();
        // 获取当前版本信息，没有则提示
        Integer version = ruleBindingService.selectCurrentContentVersion();
        if (null == version || version < 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(NO_VERSION_MSG);
            return result;
        }
        ruleBindingService.bindTableRule(ruleId, extend, tableId, version);
        return result;
    }

    @IgnoreAuthority
    @ResponseBody
    @RequestMapping(value = "/bindColumnRule.json")
    public RestServiceResult<Object> bindColumnRule(
            @RequestParam(defaultValue = "", required = true, value = "ruleId") int ruleId,
            @RequestParam(defaultValue = "", required = true, value = "extend") String extend,
            @RequestParam(defaultValue = "", required = true, value = "tableId") String tableId,
            @RequestParam(defaultValue = "", required = true, value = "fieldId") int fieldId,
            @RequestParam(defaultValue = "", required = true, value = "fieldCode") String fieldCode) {
        RestServiceResult<Object> result = new RestServiceResult<>();
        // 获取当前版本信息，没有则提示
        Integer version = ruleBindingService.selectCurrentContentVersion();
        if (null == version || version < 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(NO_VERSION_MSG);
            return result;
        }
        tableId = ("".equals(tableId)) ? "0" : tableId;
        ruleBindingService.bindColumnRule(ruleId, extend, fieldId, fieldCode, tableId, version);
        return result;
    }

    @IgnoreAuthority
    @ResponseBody
    @RequestMapping(value = "/deleteBindColumnRule.json")
    public RestServiceResult<Object> deleteBindColumnRule(
            @RequestParam(defaultValue = "", required = true, value = "ruleId") int ruleId,
            @RequestParam(defaultValue = "", required = true, value = "tableId") Integer tableId,
            @RequestParam(defaultValue = "", required = true, value = "fieldId") int fieldId) {
        RestServiceResult<Object> result = new RestServiceResult<>();
        // 获取当前版本信息，没有则提示
        Integer version = ruleBindingService.selectCurrentContentVersion();
        if (null == version || version < 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(NO_VERSION_MSG);
            return result;
        }
//        tableId = (tableId == 0) ? null : tableId;

        try {
            ruleBindingService.deleteBindColumnRule(ruleId, tableId, fieldId, version);
        } catch (Exception e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @IgnoreAuthority
    @ResponseBody
    @RequestMapping(value = "/deleteBindTableRule.json")
    public RestServiceResult<Object> deleteBindColumnRule(
            @RequestParam(defaultValue = "", required = true, value = "ruleId") int ruleId,
            @RequestParam(defaultValue = "", required = true, value = "tableId") int tableId) {
        RestServiceResult<Object> result = new RestServiceResult<>();
        // 获取当前版本信息，没有则提示
        Integer version = ruleBindingService.selectCurrentContentVersion();
        if (null == version || version < 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(NO_VERSION_MSG);
            return result;
        }
        ruleBindingService.deleteBindTableRule(ruleId, tableId, version);
        return result;
    }

    /**
     * 查看表规则是否有绑定
     *
     * @param table_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectTableBinding.json")
    @Deprecated
    public RestServiceResult<List<Integer>> selectTableBinding(
            @RequestParam(defaultValue = "", required = true, value = "table_id") int table_id) {
        RestServiceResult<List<Integer>> result = new RestServiceResult<>();
        // 获取当前版本信息，没有则提示
        Integer version = ruleBindingService.selectCurrentContentVersion();
        if (null == version || version < 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(NO_VERSION_MSG);
            return result;
        }
        List<Integer> ruleIds = ruleBindingService.selectTableBinding(table_id, version);
        result.setData(ruleIds);
        return result;
    }

    /**
     * 查看字段规则是否绑定
     *
     * @param filed_code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectColmnBinding.json")
    @Deprecated
    public RestServiceResult<List<Map<String, Object>>> selectColmnBinding(
            @RequestParam(defaultValue = "", required = true, value = "filed_code") String filed_code) {
        RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<>();
        // 获取当前版本信息，没有则提示
        Integer version = ruleBindingService.selectCurrentContentVersion();
        if (null == version || version < 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(NO_VERSION_MSG);
            return result;
        }
        List<Map<String, Object>> ruleIds = ruleBindingService.selectColmnBinding(filed_code, version);
        result.setData(ruleIds);
        return result;
    }

}
